﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Collections.ObjectModel;
using cecbClasses.Data_Adapters;
using cecbData;

namespace CECB
{
    /// <summary>
    /// Interaction logic for Project_Questions_Window.xaml
    /// </summary>
    public partial class Project_Questions_Window : Window
    {
        private List<Student> student;

        public ObservableCollection<Impacts> ModelListe { get; set; }
        Impacts model;

        public Project_Questions_Window()
        {
            InitializeComponent();
            isprojectSelect = false;
            ModelListe = new ObservableCollection<Impacts>();
            DataContext = this;

            Dss.Methods.Slider slid = new Dss.Methods.Slider();
            Dss.Methods.ProjectQuestions pq = new Dss.Methods.ProjectQuestions();
            ProjectAutocompleteTextBox.ItemsSource = slid.getProjectDetails();
            comboBoxComponents.ItemsSource = new DSSEntities().Components;
            if (listBoxComponents.Items.Count > 0)
            {
                ListGroupBoxWrap.Visibility = System.Windows.Visibility.Visible;
            }
            else ListGroupBoxWrap.Visibility = System.Windows.Visibility.Hidden;
        }

        private void ReferenceTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Dss.Methods.Slider slid = new Dss.Methods.Slider();


            //  (FindResource("drop") as Storyboard).Begin(this);


            ProjectDetailsWrapPanel.DataContext = slid.getProjectDetailsByRef(ProjectAutocompleteTextBox.Text);
            isprojectSelect = true;
            ModelListe.Clear();
        }
       
        private void button1_Click(object sender, RoutedEventArgs e)
        {

            if (!isprojectSelect)
            {
                MessageBox.Show("Select a project to add Impacts", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else
            {
                if (comboBoxComponents.SelectedIndex != -1)
                {
                    if (textBoxQuestion.Text.Length != 0)
                    {
                        model = new Impacts();

                        model.impt_name = textBoxQuestion.Text;
                        model.impt_reference = comboBoxComponents.Text;
                        ModelListe.Add(model);
                        textBoxQuestion.Clear();
                    }
                    else MessageBox.Show("Please Enter a impact", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else MessageBox.Show("Please Select a component", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.SelectedIndex > -1)
                ModelListe.RemoveAt(dataGrid1.SelectedIndex);
            else
                MessageBox.Show("Nothing has selected to remove", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }


        bool isprojectSelect = false;
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (isprojectSelect)
            {
                if (dataGrid1.Items.Count != 0)
                {
                    if (comboBoxComponents.SelectedIndex != -1)
                    {

                        List<String> Impc = new List<string>();
                        List<String> ImpcComp = new List<string>();

                        int rows = dataGrid1.Items.Count;
                        int rows1 = dataGrid1.Items.Count;
                        foreach (Impacts row in dataGrid1.Items)
                        {

                            Impc.Add(row.impt_name);
                            ImpcComp.Add(row.impt_reference);
                            rows--;
                            if (rows == 0)
                                break;
                        }

                        Dss.Methods.ProjectQuestions pq = new Dss.Methods.ProjectQuestions();
                        if (new Func_Impacts().InsertImpact(Impc, ImpcComp, rows1, labelProReference.Content.ToString()))
                        {
                            MessageBox.Show(dataGrid1.Items.Count + " Impaces Added Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            ModelListe.Clear();


                            textBoxQuestion.Text = "";

                        }
                    }
                    else
                        MessageBox.Show("select the phace", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                }
                else MessageBox.Show("Add Impacts", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            else MessageBox.Show("Select a project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);



        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {
            label3.Content = "";
            label5.Content = "";

            label6.Content = "";
            labelProReference.Content = "";
            listBoxComponents.ItemsSource = null;
            listBoxComponents.Items.Clear();
            ModelListe.Clear();
            ProjectAutocompleteTextBox.Text = "";
            textBoxComponentSearch.Text = "";
            textBoxQuestion.Text = "";
        }

        private void DataGridImpacts_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {



        }

        private void textBoxComponentSearch_TextChanged(object sender, TextChangedEventArgs e)
        {


            Dss.Methods.ProjectQuestions pq = new Dss.Methods.ProjectQuestions();
            (FindResource("drop") as Storyboard).Begin(this);
            listBoxComponents.ItemsSource = new Func_Impacts().getImpactDetailsByName(textBoxComponentSearch.Text.Trim());
            if (listBoxComponents.Items.Count > 0)
                ListGroupBoxWrap.Visibility = System.Windows.Visibility.Visible;

            else ListGroupBoxWrap.Visibility = System.Windows.Visibility.Hidden;

        }

        private void textBoxQuestion_TextChanged(object sender, TextChangedEventArgs e)
        {
            Dss.Methods.ProjectQuestions pq = new Dss.Methods.ProjectQuestions();
            (FindResource("drop") as Storyboard).Begin(this);
            listBoxComponents.ItemsSource = new Func_Impacts().getImpactDetailsByName(textBoxQuestion.Text.Trim());
            if (listBoxComponents.Items.Count > 0)
                ListGroupBoxWrap.Visibility = System.Windows.Visibility.Visible;

            else ListGroupBoxWrap.Visibility = System.Windows.Visibility.Hidden;
        }

        private void listBoxComponents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (isprojectSelect)
            {

                List<Impacts> list = new List<Impacts>();

                if (listBoxComponents.SelectedIndex != -1)
                {
                    Impacts name = (Impacts)listBoxComponents.SelectedItem;
                    String x = name.impt_component;
                    DSSEntities cecbContext = new DSSEntities();
                    String y = cecbContext.Components.First(i => i.cmpt_reference == x).cmpt_name.ToString();

                    model = new Impacts();

                    //  model.impt_name= comboBoxComponents.Text;
                    try
                    {
                        model.impt_name = name.impt_name;
                        model.impt_reference = y;
                        ModelListe.Add(model);
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            else {

                MessageBox.Show("Select a project to add Impacts","Message",MessageBoxButton.OK,MessageBoxImage.Error);
            }

        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }
    }

    public class Student
    {

        public Student(int id, String firstName, String lastName)
        {

            ID = id;

            FirstName = firstName;

            LastName = lastName;

        }



        public int ID

        { get; set; }



        public String FirstName

        { get; set; }



        public String LastName

        { get; set; }

    }
}
